﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SelectBoxMng : MonoBehaviour
{
    private RectTransform line0;
    private RectTransform line1;
    private RectTransform line2;
    private RectTransform line3;

    private DragPoint point0;
    private DragPoint point1;
    private DragPoint point2;
    private DragPoint point3;
    private Vector2[] pointPos;

// Use this for initialization
    void Awake()
    {
        this.line0 = this.transform.GetChild(4).GetComponent<RectTransform>();
        this.line1 = this.transform.GetChild(5).GetComponent<RectTransform>();
        this.line2 = this.transform.GetChild(6).GetComponent<RectTransform>();
        this.line3 = this.transform.GetChild(7).GetComponent<RectTransform>();

        this.point0 = this.transform.GetChild(0).GetComponent<DragPoint>();
        this.point1 = this.transform.GetChild(1).GetComponent<DragPoint>();
        this.point2 = this.transform.GetChild(2).GetComponent<DragPoint>();
        this.point3 = this.transform.GetChild(3).GetComponent<DragPoint>();

        this.point0.SelectBoxMng = this;
        this.point1.SelectBoxMng = this;
        this.point2.SelectBoxMng = this;
        this.point3.SelectBoxMng = this;

        this.point0.Index = 0;
        this.point1.Index = 1;
        this.point2.Index = 2;
        this.point3.Index = 3;
        this.pointPos = new Vector2[4];
    }

    public void FlashPointsPos(int index)
    {
        Vector2 startPointPos = this.transform.GetChild(index).position;

        switch (index)
        {
            case 0:
                this.point1.transform.position = new Vector2(this.point1.transform.position.x, startPointPos.y);
                this.point3.transform.position = new Vector2(startPointPos.x, this.point3.transform.position.y);
                break;
            case 1:
                this.point0.transform.position = new Vector2(this.point0.transform.position.x, startPointPos.y);
                this.point2.transform.position = new Vector2(startPointPos.x, this.point2.transform.position.y);
                break;
            case 2:
                this.point1.transform.position = new Vector2(startPointPos.x, this.point1.transform.position.y);
                this.point3.transform.position = new Vector2(this.point3.transform.position.x, startPointPos.y);
                break;
            case 3:
                this.point0.transform.position = new Vector2(startPointPos.x, this.point0.transform.position.y);
                this.point2.transform.position = new Vector2(this.point2.transform.position.x, startPointPos.y);
                break;
        }

        this.DrawLine();

        DrawRectangleMng.Instance.FlashIron(this.pointPos);
    }

    private void DrawLine()
    {
        this.pointPos[0] = this.point0.transform.position;
        this.pointPos[1] = this.point1.transform.position;
        this.pointPos[2] = this.point2.transform.position;
        this.pointPos[3] = this.point3.transform.position;

        this.line0.position = (this.pointPos[0] + this.pointPos[1]) / 2;
        this.line0.sizeDelta = new Vector2(Vector2.Distance(this.pointPos[0], this.pointPos[1]), 5);
        this.line1.position = (this.pointPos[1] + this.pointPos[2]) / 2;
        this.line1.sizeDelta = new Vector2(Vector2.Distance(this.pointPos[1], this.pointPos[2]), 5);
        this.line2.position = (this.pointPos[2] + this.pointPos[3]) / 2;
        this.line2.sizeDelta = this.line0.sizeDelta;
        this.line3.position = (this.pointPos[3] + this.pointPos[0]) / 2;
        this.line3.sizeDelta = this.line1.sizeDelta;
    }

    public void InitPointsPos(Vector2[] pos)
    {
        this.point0.transform.position = pos[0];
        this.point1.transform.position = pos[1];
        this.point2.transform.position = pos[2];
        this.point3.transform.position = pos[3];

        this.DrawLine();
    }
}
